<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Script builder</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_addon_build.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Script builder </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p><b>Path:</b> /sdk/add_on/scriptbuilder/</p>
<p>This class is a helper class for loading and building scripts, with a basic pre-processor that supports conditional compilation, include directives, and metadata declarations.</p>
<p>By default the script builder resolves include directives by loading the included file from the relative directory of the file it is included from. If you want to do this in another way, then you should implement the <a class="el" href="doc_addon_build.html#doc_addon_build_1_1">include callback</a> which will let you process the include directive in a custom way, e.g. to load the included file from memory, or to support multiple search paths. The include callback should call the AddSectionFromFile or AddSectionFromMemory to include the section in the current build.</p>
<p>If you do not want process metadata then you can compile the add-on with the define AS_PROCESS_METADATA 0, which will exclude the code for processing this. This define can be made in the project settings or directly in the header.</p>
<h2><a class="anchor" id="doc_addon_build_1"></a>
Public C++ interface</h2>
<div class="fragment"><pre class="fragment"><span class="keyword">class </span>CScriptBuilder
{
<span class="keyword">public</span>:
  <span class="comment">// Start a new module</span>
  <span class="keywordtype">int</span> StartNewModule(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine, <span class="keyword">const</span> <span class="keywordtype">char</span> *moduleName);

  <span class="comment">// Load a script section from a file on disk</span>
  <span class="keywordtype">int</span> AddSectionFromFile(<span class="keyword">const</span> <span class="keywordtype">char</span> *filename);

  <span class="comment">// Load a script section from memory</span>
  <span class="keywordtype">int</span> AddSectionFromMemory(<span class="keyword">const</span> <span class="keywordtype">char</span> *scriptCode, 
                           <span class="keyword">const</span> <span class="keywordtype">char</span> *sectionName = <span class="stringliteral">&quot;&quot;</span>);

  <span class="comment">// Build the added script sections</span>
  <span class="keywordtype">int</span> BuildModule();

  <span class="comment">// Returns the current module</span>
  <a class="code" href="classas_i_script_module.html" title="The interface to the script modules.">asIScriptModule</a> *GetModule();

  <span class="comment">// Register the callback for resolving include directive</span>
  <span class="keywordtype">void</span> SetIncludeCallback(INCLUDECALLBACK_t callback, <span class="keywordtype">void</span> *userParam);

  <span class="comment">// Add a pre-processor define for conditional compilation</span>
  <span class="keywordtype">void</span> DefineWord(<span class="keyword">const</span> <span class="keywordtype">char</span> *word);

  <span class="comment">// Get metadata declared for class types and interfaces</span>
  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForType(<span class="keywordtype">int</span> typeId);

  <span class="comment">// Get metadata declared for functions</span>
  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForFunc(<span class="keywordtype">int</span> funcId);

  <span class="comment">// Get metadata declared for global variables</span>
  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForVar(<span class="keywordtype">int</span> varIdx);

  <span class="comment">// Get metadata declared for a class method</span>
  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForTypeMethod(<span class="keywordtype">int</span> typeId, <span class="keywordtype">int</span> mthdIdx);

  <span class="comment">// Get metadata declared for a class property</span>
  <span class="keyword">const</span> <span class="keywordtype">char</span> *GetMetadataStringForTypeProperty(<span class="keywordtype">int</span> typeId, <span class="keywordtype">int</span> varIdx);
};
</pre></div><h3><a class="anchor" id="doc_addon_build_1_1"></a>
The include callback signature</h3>
<div class="fragment"><pre class="fragment"><span class="comment">// This callback will be called for each #include directive encountered by the</span>
<span class="comment">// builder. The callback should call the AddSectionFromFile or AddSectionFromMemory</span>
<span class="comment">// to add the included section to the script. If the include cannot be resolved</span>
<span class="comment">// then the function should return a negative value to abort the compilation.</span>
<span class="keyword">typedef</span> int (*INCLUDECALLBACK_t)(<span class="keyword">const</span> <span class="keywordtype">char</span> *include, <span class="keyword">const</span> <span class="keywordtype">char</span> *from, CScriptBuilder *builder, <span class="keywordtype">void</span> *userParam);
</pre></div><h2><a class="anchor" id="doc_addon_build_2"></a>
Include directives</h2>
<p>Example script with include directive:</p>
<pre>
  #include "commonfuncs.as"</pre><pre>  void main()
  {
    // Call a function from the included file
    CommonFunc();
  }
</pre><h2><a class="anchor" id="doc_addon_build_condition"></a>
Conditional programming</h2>
<p>The builder supports conditional programming through the #if/#endif preprocessor directives. The application may define a word with a call to DefineWord(), then the scripts may check for this definition in the code in order to include/exclude parts of the code.</p>
<p>This is especially useful when scripts are shared between different binaries, for example, in a client/server application.</p>
<p>Example script with conditional compilation:</p>
<pre>
  class CObject
  {
    void Process()
    {
  #if SERVER
      // Do some server specific processing
  #endif</pre><pre>  #if CLIENT
      // Do some client specific processing
  #endif</pre><pre>      // Do some common processing
    }
  }
</pre><h2><a class="anchor" id="doc_addon_build_metadata"></a>
Metadata in scripts</h2>
<p>Metadata can be added before script class, interface, function, and global variable declarations. The metadata is removed from the script by the builder class and stored for post build lookup by the type id, function id, or variable index.</p>
<p>Exactly what the metadata looks like is up to the application. The builder class doesn't impose any rules, except that the metadata should be added between brackets []. After the script has been built the application can obtain the metadata strings and interpret them as it sees fit.</p>
<p>Example script with metadata:</p>
<pre>
  [factory func = CreateOgre]
  class COgre
  {
    [editable] 
    vector3 myPosition;</pre><pre>    [editable [10, 100]]
    int     myStrength;
  }</pre><pre>  [factory]
  COgre @CreateOgre()
  {
    return @COgre();
  }
</pre><p>Example usage:</p>
<div class="fragment"><pre class="fragment">CScriptBuilder builder;
<span class="keywordtype">int</span> r = builder.StartNewModule(engine, <span class="stringliteral">&quot;my module&quot;</span>);
<span class="keywordflow">if</span>( r &gt;= 0 )
  r = builder.AddSectionFromMemory(script);
<span class="keywordflow">if</span>( r &gt;= 0 )
  r = builder.BuildModule();
<span class="keywordflow">if</span>( r &gt;= 0 )
{
  <span class="comment">// Find global variables that have been marked as editable by user</span>
  <a class="code" href="classas_i_script_module.html" title="The interface to the script modules.">asIScriptModule</a> *mod = engine-&gt;<a class="code" href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4" title="Return an interface pointer to the module.">GetModule</a>(<span class="stringliteral">&quot;my module&quot;</span>);
  <span class="keywordtype">int</span> count = mod-&gt;<a class="code" href="classas_i_script_module.html#a87e29773f7e6f2980d75288faaa74d02" title="Returns the number of global variables in the module.">GetGlobalVarCount</a>();
  <span class="keywordflow">for</span>( <span class="keywordtype">int</span> n = 0; n &lt; count; n++ )
  {
    <span class="keywordtype">string</span> metadata = builder.GetMetadataStringForVar(n);
    <span class="keywordflow">if</span>( metadata == <span class="stringliteral">&quot;editable&quot;</span> )
    {
      <span class="comment">// Show the global variable in a GUI</span>
      ...
    }
  }
}
</pre></div> </div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:03 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
